import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YCKOKO
 * Date: 2022-10-27
 * Time: 10:35
 */
public class MyArrayList {
    private int[] elem;    //elem 数组

    private int usedSize;  //usedSize记录有效的数据个数
    private static final int DEFAULT_SIZE = 10;  //默认elem数组的元素个数为10

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }
    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    // 新增元素,默认在数组最后新增
    public boolean isFull() {
        if(this.size() >= this.elem.length){
            return true;
        }
        return false;
    }

    public void add(int data) {
        //1.检查当前数组是否满了
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
        }
        this.elem[usedSize] = data;
        usedSize++;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) throws posWrongfulException{
        if(pos < 0 || pos > this.usedSize){
            throw new posWrongfulException("输入的指定位置pos不合法！");
        }
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
        }
        for (int i = usedSize-1; i >= pos ; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        usedSize ++;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        if(isFull()){
            throw new isEmptyException("当前顺序表为空！");
        }
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    public boolean isEmpty(){
        if(this.size() == 0){
            return true;
        }
        return false;
    }
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int get(int pos) {
        if(isEmpty()) {
            throw new isEmptyException("当前顺序表为空！");
        }
        if(pos < 0 || pos >= this.usedSize) {
            throw new posWrongfulException("get获取元素的时候，pos不合法异常！");
        }
        return this.elem[pos];
    }

    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        if(isEmpty()) {
            throw new isEmptyException("当前顺序表为空！");
        }
        if(pos < 0 || pos >= this.usedSize) {
            throw new posWrongfulException("set获取元素的时候，pos不合法异常！");
        }

        this.elem[pos] = value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        if(isEmpty()) {
            throw new isEmptyException("顺序表为空！");
        }
        int index = this.indexOf(toRemove);
        if(index != -1){
            for (int i = index; i < this.usedSize; i++) {
                this.elem[i] = this.elem[i+1];
            }
            this.usedSize--;
        }
    }
    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
}
